<template>
	<view class="pp">
		<wd-swiper :list="swiperList" autoplay :current="0"></wd-swiper>

		<view class="card">
			<view class="card-titles">
				<text class="tji">数据统计</text>
				<view class="tjiview">
					<view class="jinri " :class="day=='0'?'jactive':''" @click="gethead('0')">今日</view>
					<view class="jinri" :class="day=='1'?'jactive':''" @click="gethead('1')">两日</view>
					<view class="jinri" :class="day=='7'?'jactive':''" @click="gethead('7')">一周</view>
				</view>
			</view>
		</view>
		<view class="nlist">
			<view class="nitem">
				<view class="nitemone">
					<image src="/static/icon/rone.png" class="tuicon"></image>
					<view class="rval">
						<text class="xname">新增会员</text>
						<view class="shuview">
							<text class="nums">{{ info.VipQty || 0 }}个</text>
						</view>
					</view>
				</view>
				<!-- <view class="xiaofei">消费次数：1</view> -->
			</view>
			<view class="nitem">
				<view class="nitemone">
					<image src="/static/icon/rtwo.png" class="tuicon"></image>
					<view class="rval">
						<text class="xname">设备运行次数</text>
						<view class="shuview">
							<text class="nums">{{ info.TotQty || 0 }}次</text>
						</view>
					</view>
				</view>
			</view>
			<view class="nitem" v-for="(item,index) in info.comp" :key="index" @click="viewData(item)">
				<view class="nitemone">
					<image src="/static/icon/rthree.png" class="tuicon" v-if="item.compent=='点阵波'"></image>
					<image src="/static/icon/rfour.png" class="tuicon" v-else-if="item.compent=='智能热疗'"></image>
					<image src="/static/icon/rsix.png" class="tuicon" v-else-if="item.compent=='生物微电'"></image>
					<image src="/static/icon/rfive.png" class="tuicon" v-else></image>

					<view class="rval">
						<text class="xname">{{ item.compent }}</text>
						<view class="shuview">
							<text class="nums">{{ item.qty || 0 }}</text>
							{{ index == 0 ? '个' : '次' }}
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="sheview">
			<view class="sheitem" @click="getshe(1)">
				<image src="/static/icon/rseven.png" class="sheimg"></image>
				<text>我的设备</text>
			</view>
			<view class="sheitem" @click="getshe(2)">
				<image src="/static/icon/reight.png" class="sheimg"></image>
				<text>我的会员</text>
			</view>
		</view>
		<wd-overlay :show="popshow">
			<view class="wrapper">
				<view class="pview">
					<view class="tname">
						<view style="width: 100%;">
							{{anntMsgList.titile||''}}
						</view>
					</view>
					<scroll-view style="max-height: 550rpx;" scroll-y="true">
						<view class="tcont">
							<rich-text :nodes="anntMsgList.msgContent"></rich-text>
						</view>
					</scroll-view>
					<view class="guan" @click="getshoudao">收到</view>
				</view>
			</view>
		</wd-overlay>
	</view>
</template>
<script setup>
	import {
		computed,
		ref,
		reactive
	} from 'vue';
	import userApiService from "@/api/user.js"
	import apiService from "@/api/server.js"
	import {
		onLoad,
		onShow,
		onPullDownRefresh
	} from '@dcloudio/uni-app'

	const noticeTotal = ref(0)
	const swiperList = ref([])
	const anntMsgList = ref({})
	const popshow = ref(false)

	onLoad(() => {

		apiService.getHome().then(res => {
			let list = []
			res.banner.map(item => {
				list.push(item.image)
			})
			swiperList.value = list
		})
	})
	const viewData = (e) => {
		console.log(e)
		uni.navigateTo({
			url: `/pages/equipment/runnnerFilter?day=${day}&compent=${e.compent}`
		})
	}

	onShow(() => {
		// #ifdef H5
		// 模拟登录
		let token = 'STWkiHepGJL/duFwdMh35Q==';
		let userId = '32'
		let userName = 'a'
		let eTokentoken =
			'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MjU3NDA5MDAsInVzZXJuYW1lIjoiMTc2Mzg1Mzg2NDUifQ.qawHAvhu0ArYcDOEStU_Wef8wIUYek80rgUXWgAHMgA'
		uni.setStorageSync("token", token)
		uni.setStorageSync("userId", userId)
		uni.setStorageSync("username", userName)
		uni.setStorageSync("etoken", eTokentoken)
		// #endif

		userApiService.getAnnountCement().then(res => {
			noticeTotal.value = res.anntMsgTotal
		})
		// queryTotData
		getgonggao()
		getinit()
	})

	function getgonggao() {
		userApiService.listByUsernew({}).then(res => {
			console.log(res)
			if (res.anntMsgList != '' && res.anntMsgList) {
				anntMsgList.value = res.anntMsgList[0]
				popshow.value = true

			}
		})
	}

	function getshoudao() {
		console.log(anntMsgList.value.id)
		userApiService.editByAnntIdAndUserId({
			anntId: anntMsgList.value.id
		}).then(res => {
			console.log(res)
			popshow.value = false
		})
	}
	var day = reactive('0')
	const info = reactive({
		TotQty: 0,
		VipQty: 0,
		comp: []
	})

	function getinit() {
		// 今日    0    昨天  1    本周  7
		userApiService.queryTotData({
			day: day
		}).then((res) => {
			info.TotQty = res.TotQty
			info.VipQty = res.VipQty
			info.comp = res.comp
		})
	}

	function gethead(e) {
		day = e
		getinit()
	}

	function getshe(e) {
		if (e == 1) {
			//我的设备
			uni.navigateTo({
				url: '/pages/form/equipment'
			})
		} else if (e == 2) {
			//会员
			uni.navigateTo({
				url: '/pages/user/index'
			})
		}
	}


	const primary_list = [{
			title: "保修列表",
			icon: "http://t.tbx-tec.cn/cdn/gengxinjihua.svg",
			url: "/pages/maintenance/index"
		},
		{
			title: "会员管理",
			icon: "http://t.tbx-tec.cn/cdn/tongjijilu.svg",
			url: "/pages/user/index"
		},
		{
			title: "商户",
			icon: "http://t.tbx-tec.cn/cdn/yunhangguanli.svg",
			url: "/pages/form/shop"
		},
		{
			title: "门店",
			icon: "http://t.tbx-tec.cn/cdn/yunhangguanli.svg",
			url: "/pages/form/stores"
		},
		{
			title: "设备",
			icon: "http://t.tbx-tec.cn/cdn/yunhangguanli.svg",
			url: "/pages/form/equipment"
		},
		{
			title: "设备运行记录",
			icon: "http://t.tbx-tec.cn/cdn/yunhangguanli.svg",
			url: "/pages/form/equipment"
		},
	]

	const notice = computed(() => {
		return {
			title: "通知公告",
			icon: "http://t.tbx-tec.cn/cdn/yunhangguanli.svg",
			url: "/pages/notice/list",
			bridge: {
				value: noticeTotal.value,
				max: 10,
			}
		}
	})

	const user_list = [{
			title: "培训管理",
			icon: "http://t.tbx-tec.cn/cdn/zhengmingziliaoshuomingshu.svg",
			url: "https://ms.tbx-tec.cn/?name=test"
		},
		{
			title: "门店进货",
			icon: "http://t.tbx-tec.cn/cdn/baoyangguanli.svg",
			url: "/pages/shop/index"
		},
		{
			title: "地址管理",
			icon: "http://t.tbx-tec.cn/cdn/yunhangguanli.svg",
			url: "/pages/user/address"
		},
		{
			title: "日报",
			icon: "http://t.tbx-tec.cn/cdn/yunhangguanli.svg",
			url: "/pages/form/daily"
		},
		{
			title: "拜访",
			icon: "http://t.tbx-tec.cn/cdn/yunhangguanli.svg",
			url: "/pages/form/visit"
		},
	]

	const click = (e) => {
		if (e.url === "") {
			return uni.showToast({
				title: "功能尚未开放，请耐心等待",
				icon: 'none'
			})
		}
		if (e.url.slice(0, 4) == "http") {
			uni.navigateTo({
				url: `/pages/index/webview?url=${encodeURIComponent(e.url)}`
			})
			return
		}

		uni.navigateTo({
			url: e.url
		})
	}
</script>

<style lang="scss" scoped>
	.bg-white {
		background-color: #fff;
	}

	.card {
		margin-top: 20rpx;
		padding: 20rpx;

		&-title {
			font-size: 42rpx;
			font-weight: 600;
		}

		&-box {
			display: grid;
			grid-template-columns: 4fr;
			grid-gap: 26px 30px;
			justify-items: center;
			color: #666;

			&-item {}
		}

		.slot-img {
			width: 80rpx;
			height: 80rpx;
		}
	}

	.card-titles {
		display: flex;
		flex-direction: row;
		align-items: center;

		.tji {
			font-size: 40rpx;
			font-weight: 500;
			color: #333;
		}

		.tjiview {
			margin-left: auto;
			display: flex;
			flex-direction: row;
			align-items: center;

			.jinri {
				padding: 4rpx 15rpx;
				background-color: #ccc;
				color: #fff;
				font-size: 28rpx;
				font-weight: normal;
				margin-left: 20rpx;
			}

			.jactive {
				background-color: #007aff;
				color: #fff;
			}
		}
	}

	.nlist {
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
		margin-top: 20rpx;

		.nitem {
			width: 48%;
			height: 140rpx;
			background-color: #fff;
			border-radius: 10rpx;
			// padding: 30rpx;
			display: flex;
			flex-direction: column;
			justify-content: center;
			margin-bottom: 30rpx;

			.nitemone {
				display: flex;
				flex-direction: row;
				align-items: center;
				margin: 0 30rpx;

				.tuicon {
					width: 80rpx;
					height: 80rpx;
					border-radius: 50%;
				}

				.rval {
					display: flex;
					flex-direction: column;
					flex: 1;
					margin-left: 20rpx;

					.xname {
						color: #333;
						font-size: 32rpx;
						font-weight: 500;
					}

					.shuview {
						display: flex;
						flex-direction: row;
						margin-top: 8rpx;
						align-items: center;

						.nums {
							color: #333;
							font-weight: 500;
							font-size: 34rpx;
						}
					}
				}
			}

		}

		.xiaofei {
			border-top: 1rpx solid #eee;
			display: flex;
			align-items: center;
			color: #333;
			font-size: 32rpx;
			font-weight: normal;
			margin-top: 14rpx;
			padding: 10rpx 30rpx;
		}

		.nitem:nth-child(even) {
			margin-left: auto;
		}
	}

	.sheview {
		display: flex;
		flex-direction: row;
		align-items: center;
		margin: 30rpx;

		.sheitem {
			display: flex;
			width: 50%;
			flex-direction: column;
			align-items: center;
			justify-content: center;

			.sheimg {
				width: 120rpx;
				height: 120rpx;
				margin-bottom: 10rpx;
			}

			text {
				color: #333;
				font-size: 32rpx;
				font-weight: 500;
			}
		}
	}

	.wrapper {

		display: flex;
		align-items: center;
		justify-content: center;
		height: 100%;

		.pview {
			background-color: #fff;
			border-radius: 30rpx 30rpx;
			// padding: 30rpx;
			width: 80%;

			.tname {
				width: 100%;
				height: 100rpx;
				background-color: #007aff;
				color: #fff;
				font-size: 35rpx;
				font-weight: 500;
				border-radius: 30rpx 30rpx 0 0;
				display: flex;
				align-items: center;
				padding: 0 20rpx;
				box-sizing:border-box;

				// justify-content: center;
				view {
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap; //不折行
				}
			}
		}

		.tcont {
			padding: 30rpx;
			color: #333;
			font-size: 28rpx;
		}

		.guan {
			color: #FFF;
			background-color: #007aFF;
			color: #fff;
			font-size: 30rpx;
			border-radius: 30rpx;
			margin: 30rpx;
			height: 88rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}
</style>